package com.cf.expire;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * game-server-DefaultExpireService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/29 下午 08:59
 */
public class DefaultExpireService implements ExpireService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public boolean setExpire(String key, String data, Integer second) {
        Boolean isStetted = redisTemplate.opsForValue().setIfAbsent(key, data);
        if (isStetted) {
            redisTemplate.expire(key, second, TimeUnit.SECONDS);
        }
        return isStetted;
    }

    @Override
    public String getExpire(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void del(String key) {
        redisTemplate.delete(key);
    }
}
